package com.util;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Pattern;

public class Validate {

	//清除SQL代码
	public static String replaceHtml(String HTMLStr) {
		String htmlStr = HTMLStr;
		String textStr = "";
		java.util.regex.Pattern p_script;
		java.util.regex.Matcher m_script;
		java.util.regex.Pattern p_style;
		java.util.regex.Matcher m_style;
		java.util.regex.Pattern p_html;
		java.util.regex.Matcher m_html;
		try {
			String regEx_script = "<[//s]*?script[^>]*?>[//s//S]*?<[//s]*?///[//s]*?script[//s]*?>";
			String regEx_style = "<[//s]*?style[^>]*?>[//s//S]*?<[//s]*?///[//s]*?style[//s]*?>";
			String regEx_html = "<[^>]+>";
			p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
			m_script = p_script.matcher(htmlStr);
			htmlStr = m_script.replaceAll("");
			p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
			m_style = p_style.matcher(htmlStr);
			htmlStr = m_style.replaceAll("");
			p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
			m_html = p_html.matcher(htmlStr);
			htmlStr = m_html.replaceAll("");
			textStr = htmlStr.replaceAll(" ", "");
			textStr = htmlStr.replaceAll("<", "<");
			textStr = htmlStr.replaceAll(">", ">");
			textStr = htmlStr.replaceAll("?", "?");
			textStr = htmlStr.replaceAll("&", "&");
		} catch (Exception e) {
			// System.err.println("Html2Text: " + e.getMessage());
		}
		return textStr;
	}

	/**
	 *是否为数字和英文验证֤
	 * 
	 */
	public int getIntAndChar(String str) {
		char c;
		int i;
		for (i = 0; i < str.length(); i++) {
			c = str.charAt(i);
			if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) {
				break;
			}
		}// 判断字符是否在某个区间
		if (i < str.length()) {
			return -1;
		} else {
			return 0;
		}
	}

	/**
	 * 是否为整数验证֤
	 * 
	 */
	public int getInt(String str) {
		char c;
		int i;
		for (i = 0; i < str.length(); i++) {
			c = str.charAt(i);
			if (!(c >= '0' && c <= '9')) {
				break;
			}
		}// 判断字符是否在某个区间
		if (i < str.length()) {
			return -1;
		} else {
			return 0;
		}
	}

	/**
	 * 是否为非法字符验证
	 */
	public boolean getLawlessChar(String str) {
		boolean flag = false;
		char c;
		for (int i = 0; i < str.length(); i++) {
			c = str.charAt(i);
			switch (c) {
			case '<':
				flag = true;
				break;
			case '>':
				flag = true;
				break;
			case '"':
				flag = true;
				break;
			case '&':
				flag = true;
				break;
			case ' ':
				flag = true;
				break;
			}
		}
		return flag;
	}

	/**
	 * 中文转换 将gb2312编码转换成unicode编码
	 */
	public String getUnicode(String str) {
		if (str != null) {
			try {
				return new String(str.getBytes("iso8859_1"), "gb2312");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				return str;
			}
		} else {
			return null;
		}
	}

	/**
	 * 中文转换 将unicode编码转换成gb2312编码
	 */
	public String getGb2312(String str) {
		if (str != null) {
			try {
				return new String(str.getBytes("gb2312"), "iso8859_1");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				return str;
			}
		} else {
			return null;
		}
	}

	/**
	 *格式化日期 年-月-日 小时（0-23）:分钟:秒
	 */
	public static String getSystemDate() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		return df.format(date);
	}

	// 日期比较 早于当前日期返回TRUE ,比当前日期迟,返回假
	public static boolean getDateIfNow(String date) {
		boolean flag = false;

		try {
			String nowdate = getSystemDate();

			SimpleDateFormat formatDate = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			Date ddd = new Date();
			ddd = formatDate.parse(date); 

			Date xxx = new Date();
			xxx = formatDate.parse(nowdate); 

			flag = ddd.before(xxx);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * * DateתString * @param count
	 * 
	 * @return
	 * @throws ParseException
	 */
	public static String datemmdd(String date) {

		String str = "";

		try {
			SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");

			Date ddd = new Date();
			ddd = formatDate.parse(date);
			formatDate = new SimpleDateFormat("MM月dd日");
			str = formatDate.format(ddd);
			// String time;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	public static String datemmddtime(String date) {
		String str = "";

		try {
			SimpleDateFormat formatDate = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			Date ddd = new Date();
			ddd = formatDate.parse(date);
			formatDate = new SimpleDateFormat("MM月dd日  HH:mm");
			str = formatDate.format(ddd);
			String time;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 格式化日期 月-日
	 */
	public static String getSystemDatemmdd() {
		SimpleDateFormat df = new SimpleDateFormat("MM月dd日");
		Date date = new Date();
		return df.format(date);
	}

	/**
	 * 四舍五入
	 */
	public float getRround(float f) {
		int tem = (int) (f * 100 + 0.5);
		float ff = ((float) tem / 100);
		return ff;
	}
}
